/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EnlistmentPane.java
 *
 * Created on Jan 8, 2012, 9:58:00 PM
 */
package views.panes;

import controller.human.Person;
import controller.human.Role;
import controller.human.User;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import views.MafiaFrame;
import views.UserData;

/**
 *
 * @author darkdev
 */
public class EnlistmentPane extends javax.swing.JPanel {

    /** Creates new form EnlistmentPane */
    MafiaFrame gui;
    JPanel register;
    public EnlistmentPane(MafiaFrame gui, JPanel registerPlayers) {
        initComponents();
        this.gui = gui;
        register = registerPlayers;
        if (this.gui.settings.usePasswords==false)
        {
         this.passLabel.setVisible(false);
         this.password.setVisible(false);
        }
        else
        {
         this.passLabel.setVisible(true);
         this.password.setVisible(true);           
        }
        revalidate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        username = new javax.swing.JTextField();
        passLabel = new javax.swing.JLabel();
        password = new javax.swing.JPasswordField();
        Role = new javax.swing.JPanel();
        regUser = new javax.swing.JButton();
        cancel = new javax.swing.JButton();

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Enter your Information ");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Username:");

        username.setText("user");

        passLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        passLabel.setText("Password:");

        password.setText("Pass1");

        regUser.setText("Register");
        regUser.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                regUserMouseClicked(evt);
            }
        });
        regUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regUserActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelMouseClicked(evt);
            }
        });
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout RoleLayout = new javax.swing.GroupLayout(Role);
        Role.setLayout(RoleLayout);
        RoleLayout.setHorizontalGroup(
            RoleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RoleLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(regUser, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancel)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        RoleLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancel, regUser});

        RoleLayout.setVerticalGroup(
            RoleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RoleLayout.createSequentialGroup()
                .addGroup(RoleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(regUser)
                    .addComponent(cancel))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(passLabel)
                        .addGap(18, 18, 18)
                        .addComponent(password, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
                    .addComponent(Role, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {password, username});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel2, passLabel});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passLabel))
                .addGap(53, 53, 53)
                .addComponent(Role, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel2, passLabel});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {password, username});

    }// </editor-fold>//GEN-END:initComponents

    private void cancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelMouseClicked
        // TODO add your handling code here:
        gui.update(register);
    }//GEN-LAST:event_cancelMouseClicked

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cancelActionPerformed

    private void regUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regUserActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_regUserActionPerformed

    private void regUserMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_regUserMouseClicked
        // TODO add your handling code here:
        Person user = new Person(username.getText());
        
        if (this.gui.settings.usePasswords)
            user.setPassword(password.getPassword().toString());
        Role role = gui.addPlayer(user);
        ((PlayersRegistrationPane) register).userRegistered();
        
        String text = "Your role is: "+role;
        JLabel rolAssignment = new JLabel(text);
        Role.removeAll();
        this.Role.setLayout(new BoxLayout(this.Role, BoxLayout.PAGE_AXIS));
        this.Role.add(rolAssignment);
        next = new JButton("Next");
        next.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nextMouseClicked(evt);
            }           
        });
        this.Role.add(next);  
        revalidate();
    }//GEN-LAST:event_regUserMouseClicked
    private void nextMouseClicked(MouseEvent evt) {
       gui.update(register);
    }
    JButton next;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Role;
    private javax.swing.JButton cancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel passLabel;
    private javax.swing.JPasswordField password;
    private javax.swing.JButton regUser;
    private javax.swing.JTextField username;
    // End of variables declaration//GEN-END:variables
}
